/**
 * @Author: chenqy
 * @Date: 2020-8-26 14:06:03
 * @LastEditors chenqy
 * @LastEditTime 2020-8-26 14:06:03
 * @Description: 工具类
 */

import { saveDataInfo } from '../api/login/login.js';
// 获取用户信息
const getUserInfo = () => {
  let rt = {};
  let userInfo = uni.getStorageSync('userInfo');
  try {
    if (userInfo) {
      rt = JSON.parse(userInfo);
    }
  } catch (e) {
    console.error(e);
  }
  return rt;
};
// 设置用户信息
const setUserInfo = (infoPerson, token) => {
  let userInfo = {
    userid: infoPerson.id,
    nickname: infoPerson.nickname,
    tel: infoPerson.tel
  };
  uni.setStorageSync('userInfo', JSON.stringify(userInfo));
  uni.setStorageSync('token', token);
};
// 清除用户信息
const clearUserInfo = () => {
  uni.removeStorageSync('userInfo');
  uni.removeStorageSync('token');
};

// 计算两个日期相隔天数,传入(XX月XX日XX:XX,XXXX,XX月XX日XX:XX,XXXX)
const getDaysBetween = (startDate,startYear, endDate,endYear) => {
  // console.log(startDate, startYear,endDate,endYear)
  let start = [
    startYear,
    startDate.substring(0, 2),
    startDate.substring(3, 5),
    startDate.substring(6, 8),
    startDate.substring(9, 11),
    0,
  ];
  let end = [
    endYear,
    endDate.substring(0, 2),
    endDate.substring(3, 5),
    endDate.substring(6, 8),
    endDate.substring(9, 11),
    0,
  ];
  let startStamp=+new Date(start[0], start[1], start[2], start[3], start[4], start[5])
  let endStamp=+new Date(end[0], end[1], end[2], end[3], end[4], end[5])
  let days = Math.ceil((endStamp - startStamp) / (1 * 24 * 60 * 60 * 1000));
  return days;
};

export {
  getUserInfo,
  setUserInfo,
  clearUserInfo,
  getDaysBetween,
};
